package Padre::Task::Outline::Moose;

=head1 NAME

Padre::Task::Outline::Moose - Perl document outline structure info for Moose

=cut

use 5.008;

use strict;
use warnings;

our $VERSION = '0.01';

use base 'Padre::Task::Outline::Perl';

sub _get_outline {
    my $self = shift;
    $self->SUPER::_get_outline(@_);

#    return unless $self->{outline} eq 'ARRAY';

    require PPI::Find;
    require PPI::Document;

#require YAML;
#print STDERR YAML::Dump($self->{outline});

    my %outline_index = ();
    foreach my $n (0 .. @{$self->{outline}}-1) {
        $outline_index{ $self->{outline}->[$n]->{name} } = $n;
    };
#print STDERR YAML::Dump(\%outline_index);

    my $ppi_doc = PPI::Document->new( \$self->{text} );

    return {} unless defined($ppi_doc);

    $ppi_doc->index_locations;

    my $find = PPI::Find->new(
        sub {
            return 1 if ref $_[0] eq 'PPI::Statement'
                        or ref $_[0] eq 'PPI::Statement::Package';
        }
    );

    my @things        = $find->in($ppi_doc);
    my $cur_pkg       = {};
    my $not_first_one = 0;

    foreach my $thing (@things) {
        if (ref $thing eq 'PPI::Statement::Package') {
            if ($not_first_one) {
                if (not $cur_pkg->{name}) {
                    $cur_pkg->{name} = 'main';
                }
                push @{$self->{outline}->[ $outline_index{ $cur_pkg->{name} } ]->{methods}}, @{$cur_pkg->{methods}}
                    if (ref $cur_pkg->{methods}||'') eq 'ARRAY';
                push @{$self->{outline}->[ $outline_index{ $cur_pkg->{name} } ]->{modules}}, @{$cur_pkg->{modules}}
                    if (ref $cur_pkg->{modules}||'') eq 'ARRAY';
                $cur_pkg = {};
            }
            $not_first_one   = 1;
            $cur_pkg->{name} = $thing->namespace;
        }
        else {
            my @children = $thing->children;
            if ($children[0]->class eq 'PPI::Token::Word') {
                if ($children[0]->content =~ /^(has|around|before|after|override|extends|with)$/) {
                    my $item = $children[0]->content =~ /^(extends|with)$/ ? 'modules' : 'methods';
                    my $prefix = $children[0]->content eq 'has' ? '$' : '';
                    shift @children;
                    foreach my $node (@children) {
                        if ($node->isa('PPI::Token::Quote')) {
                            push @{$cur_pkg->{$item}}, { name => $prefix.$node->string, line => $node->location->[0] };
                            last;
                        }
                        elsif ($node->isa('PPI::Token::Word')) {
                            push @{$cur_pkg->{$item}}, { name => $prefix.$node->literal, line => $node->location->[0] };
                            last;
                        };
                    }
                }
            }
        };
    };

    if (not $cur_pkg->{name}) {
        $cur_pkg->{name} = 'main';
    }
require YAML;
print STDERR YAML::Dump($cur_pkg, \%outline_index);
    push @{$self->{outline}->[ $outline_index{ $cur_pkg->{name} } ]->{methods}}, @{$cur_pkg->{methods}}
        if (ref $cur_pkg->{methods}||'') eq 'ARRAY';
    push @{$self->{outline}->[ $outline_index{ $cur_pkg->{name} } ]->{modules}}, @{$cur_pkg->{modules}}
        if (ref $cur_pkg->{modules}||'') eq 'ARRAY';

#require YAML;
print STDERR YAML::Dump($self->{outline});

    return;
};


1;


=head1 AUTHOR

Piotr Roszatycki <dexter@cpan.org>

=head1 LICENSE

This program is free software; you can redistribute it and/or
modify it under the same terms as Perl 5 itself.
